package com.yazi.mxz.person.controller.shop;

import javax.annotation.Resource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.StoreRegion;
import com.yazi.mxz.common.entity.beans.StoreRegionId;
import com.yazi.mxz.common.service.StoreRegionService;

@Controller("shop-storeRegionController")
@RequestMapping("/shop/storeRegion")
public class StoreRegionController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreRegionController.class);
	
	@Resource
	StoreRegionService storeRegionService;
	
	@RequestMapping("/list")
	public String list(Model model) {
		debug(log, "进入配送范围列表页面");
		model.addAttribute("store", getStore(true));
		return "shop/storeRegion_list";
	}
	
	@RequestMapping("/addDo/json")
	@ResponseBody
	public StatusResult addDoJson(StoreRegion storeRegion) {
		debug(log, "添加配送地区");
		StatusResult jsonResult = new StatusResult();
		try{
			storeRegion.getId().setStoreId(getStoreId());
			storeRegionService.add(storeRegion);
			jsonResult.setOk();
		} catch (MyException e) {
			debug(log, e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "添加配送地区异常");
			jsonResult.setErrorMessage("添加失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(long regionId) {
		debug(log, "删除配送地区");
		StatusResult jsonResult = new StatusResult();
		try{
			StoreRegionId id = new StoreRegionId();
			id.setRegionId(regionId);
			id.setStoreId(getStoreId());
			StoreRegion sr = new StoreRegion();
			sr.setId(id);
			storeRegionService.delete(sr);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "删除配送地区异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
}
